//
//  LoginViewController.m
//  viewController
//
//  Created by rimi on 15/10/10.
//  Copyright (c) 2015年 ouyang. All rights reserved.
//

#import "LoginViewController.h"
#import "PreviousForgetViewController.h"
#import "RegisterViewController.h"
#define IMAGE_NAME(X) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:(X)]]



@interface LoginViewController ()

@property (nonatomic, strong)UITextField *usernameTF;
@property (nonatomic, strong)UITextField *passwordTF;
@property (nonatomic, strong) UIView *textView;
@property (nonatomic, strong)UIButton *loginButton;
@property (nonatomic, strong)UIButton *forgetButton; //跳转忘记密码按钮
@property (nonatomic, strong)UIButton *registerButton;
@property (nonatomic, strong)UIButton *leftButton;
@property (nonatomic, strong)UILabel *navLabel;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self initBackButton];
//    [self initNavTitle:@"登陆"];
    UIImageView *customBackgournd = [[UIImageView alloc] initWithImage:IMAGE_NAME(@"登录背景图.jpg")];
    customBackgournd.frame = flexibleFrame(CGRectMake(0, 0, 375, 667), NO);
    [self.view addSubview:customBackgournd];
    [self.view addSubview:self.textView];
    [self.textView addSubview:self.usernameTF];
    [self.textView addSubview:self.passwordTF];
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.forgetButton];
    [self.view addSubview:self.registerButton];
    [self.view addSubview:self.leftButton];
    [self.view addSubview:self.navLabel];
    
}


- (void)backButtonAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)textFieldDidChange:(UITextField *)textField
{
    
    if (textField == self.usernameTF) {
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }
    if (textField == self.passwordTF) {
        if (textField.text.length > 16) {
            textField.text = [textField.text substringToIndex:16];
        }
    }

}

- (void)registerButtonAction:(UIButton *)sender {
    RegisterViewController *registerView = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerView animated:YES];

}
- (void)forgetAction:(UIButton *)sender {
    PreviousForgetViewController *CFVC = [[PreviousForgetViewController alloc] init];
    [self.navigationController pushViewController:CFVC animated:YES];

}

- (UITextField *)usernameTF {
    if (!_usernameTF) {
        _usernameTF = ({
            UITextField *textField = [[UITextField alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 240, 40), NO)];
            textField.center =  flexibleCenter(CGPointMake(300 / 2, 40 + 5));
            textField.placeholder = @"请输入账号";
            textField.keyboardType = UIKeyboardTypeNumberPad;
            [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            textField.textAlignment = NSTextAlignmentLeft;
            textField;
        });
    }
    return _usernameTF;
}

- (UITextField *)passwordTF{
    if (!_passwordTF) {
        _passwordTF = ({
            UITextField *textField = [[UITextField alloc] initWithFrame:flexibleFrame(CGRectMake(0, 0, 240, 40), NO)];
            textField.center =  flexibleCenter(CGPointMake(300 / 2, 120 - 5));
            textField.placeholder = @"请输入密码";
            textField.textAlignment = NSTextAlignmentLeft;
            textField.returnKeyType = UIReturnKeyDone;
            textField.secureTextEntry = YES;
            [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            textField;
        });
    }
    return _passwordTF;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = flexibleFrame(CGRectMake(0, 0, 300, 50), NO);
            button.center =  flexibleCenter(CGPointMake(WIDTH / 2, 440));
            button.layer.cornerRadius = 10;
            button.backgroundColor = THEMECOLOR;
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setTitle:@"登录" forState:UIControlStateNormal];
            button;
        });
    }
    return _loginButton;
}

- (UIButton *)forgetButton {
    if (!_forgetButton) {
        _forgetButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = flexibleFrame(CGRectMake(0, 0, 180, 50), NO);
            button.center =  flexibleCenter(CGPointMake(WIDTH / 4, HEIGHT - 50));
            button.titleLabel.font = [UIFont systemFontOfSize:16];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setTitle:@"忘记密码" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(forgetAction:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _forgetButton;
}
- (UIView *)textView {
    if (!_textView) {
        _textView = ({
            UIView *view = [[UIView alloc] initWithFrame:flexibleFrame(CGRectMake(0, 0, 300, 150), NO)];
            view.center =  flexibleCenter(CGPointMake(375 / 2, 300));
            view.layer.cornerRadius = 10;
            UIView *line = [[UIView alloc] initWithFrame:flexibleFrame(CGRectMake(0, 0, 250, 1), NO)];
            line.center =  flexibleCenter(CGPointMake(300 / 2, 80));
            line.layer.borderWidth = 0.5;
            line.layer.borderColor = [UIColor colorWithWhite:0.828 alpha:1.000].CGColor;
            [view addSubview:line];
            view.backgroundColor = [UIColor whiteColor];
            view;
        });
    }
    return _textView;
}

- (UIButton *)registerButton {
    if (!_registerButton) {
        _registerButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = flexibleFrame(CGRectMake(0, 0, 180, 50), NO);
            button.center =  flexibleCenter(CGPointMake(WIDTH / 4 * 3, HEIGHT - 50));
            button.titleLabel.font = [UIFont systemFontOfSize:16];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setTitle:@"注册" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            button;
        
        });
    }
    return _registerButton;
}

- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = flexibleFrame(CGRectMake(12, 27, 24, 24), NO);
            [button setBackgroundImage:[UIImage imageNamed:@"返回_白色"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _leftButton;
}
- (UILabel *)navLabel {
    if (!_navLabel) {
        _navLabel = ({
            UILabel *label = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 150, 40), NO)];
            label.center =  flexibleCenter(CGPointMake(WIDTH / 2, 40));
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont italicSystemFontOfSize:22];
            label.textColor = [UIColor whiteColor];
            label.text = @"登录";
            label;
        });
    }
    return _navLabel;
}



@end
